#include "clickanimatecontrol.h"

#include <QTimer>
#include <QDebug>
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include <KF5/KWindowSystem/KWindowSystem>

ClickAnimateControl::ClickAnimateControl(double time, int diameter, bool radiant, double opacity)
{
    if(!m_info.isWlcom()){
        m_showTime = time * 1000;

        m_globalMouse = new GlobalMouse();
        connect( m_globalMouse, SIGNAL( mousePressed( int, int ) ), this, SLOT( mousePressed( int, int ) ) );
        connect( m_globalMouse, SIGNAL( mousemove( int, int ) ), this, SLOT( mousemove( int, int ) ) );

        m_animateWindow = new AnimateWindow();
        setDiameterColor( diameter, Qt::red );
        setRadiant(radiant);
        setOpacity(opacity);
    }else{
        m_dbus_interface = new QDBusInterface("com.kylin.Wlcom",
                                             "/com/kylin/Wlcom/Effect",
                                             "com.kylin.Wlcom.Effect",
                                             QDBusConnection::sessionBus());
        if (!m_dbus_interface->isValid()) {
            qDebug() << "Wlcom-DBus 连接失败：" << m_dbus_interface->lastError().message();
        }
    }
}

ClickAnimateControl::~ClickAnimateControl()
{
}

void ClickAnimateControl::pointerOnOff( bool value )
{
    if ( value == true )
        animateWindowOn();

    if ( value == false )
        animateWindowOff();
}

void ClickAnimateControl::setIsShowPath(bool value)
{
    if(!m_info.isWlcom()){
        m_isShowPath = value;
        m_animateWindow->setIsShowPath(value);
    }else{
        QDBusMessage reply = m_dbus_interface->call("EnableEffect", "mouse_trail", value);
        if (reply.type() != QDBusMessage::ReplyMessage) {
            qDebug() << "EnableEffect_mouse_trail 调用失败：" << reply.errorMessage();
        }
    }


}

void ClickAnimateControl::setIsShowClick(bool value)
{
    if(!m_info.isWlcom()){
        m_isShowClick = value;
        m_animateWindow->setIsShowClick(value);
    }else{
        QDBusMessage reply = m_dbus_interface->call("EnableEffect", "mouse_click", value);
        if (reply.type() != QDBusMessage::ReplyMessage) {
            qDebug() << "EnableEffect_mouse_click 调用失败：" << reply.errorMessage();
        }
    }
}

void ClickAnimateControl::animateWindowOn()
{
    m_globalMouse->setCursorOn();
    m_globalMouse->mousePressed();
}

void ClickAnimateControl::animateWindowOff()
{
    m_globalMouse->setCursorOff();
    if (m_animateWindow)
    {
        m_animateWindow->hide();
    }
}

void ClickAnimateControl::mousePressed( int x, int y )
{
    if (m_isShowClick)
    {
        auto screenNumber = QApplication::desktop()->screenNumber();
        QScreen *screen = QApplication::screens()[screenNumber];
        m_animateWindow->setIsPressed(true);
        m_animateWindow->setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::ToolTip );

        //华为高缩放比需要减1像素
        if(m_info.isHWWayland()){
            if (screen->devicePixelRatio() > 1.25)
            {
                m_animateWindow->move( x/screen->devicePixelRatio() - m_animateWindow->width() / 2 - 1, y/screen->devicePixelRatio() - m_animateWindow->height() / 2 - 1);
            }
            else
            {
                m_animateWindow->move( x/screen->devicePixelRatio() - m_animateWindow->width() / 2, y/screen->devicePixelRatio() - m_animateWindow->height() / 2);
            }
        }else{
            m_animateWindow->move(x/screen->devicePixelRatio() - m_animateWindow->width() / 2, y/screen->devicePixelRatio() - m_animateWindow->height() / 2);
        }

        m_animateWindow->show();
        QTimer::singleShot(m_showTime, this, SLOT( closeWindow() ) );
    }
}

void ClickAnimateControl::mousemove(int x, int y)
{
    if (m_isShowPath)
    {
        auto screenNumber = QApplication::desktop()->screenNumber();
        QScreen *screen = QApplication::screens()[screenNumber];
        m_animateWindow->setIsPressed(false);
        m_animateWindow->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);

        if(m_info.isHWWayland()){
            if (screen->devicePixelRatio() > 1.25)
            {
                m_animateWindow->move( x/screen->devicePixelRatio() - m_animateWindow->width() / 2 - 1, y/screen->devicePixelRatio() - m_animateWindow->height() / 2 - 1);
            }
            else
            {
                m_animateWindow->move( x/screen->devicePixelRatio() - m_animateWindow->width() / 2, y/screen->devicePixelRatio() - m_animateWindow->height() / 2);
            }
        }else{
            m_animateWindow->move(x/screen->devicePixelRatio() - m_animateWindow->width() / 2, y/screen->devicePixelRatio() - m_animateWindow->height() / 2);
        }


        m_animateWindow->addTrailPoint(QPoint(x/screen->devicePixelRatio(), y/screen->devicePixelRatio()));

        if(m_info.isHWWayland()){
            KWindowSystem::setType(m_animateWindow->winId(), NET::OnScreenDisplay);
        }

        m_animateWindow->show();
    }
}

void ClickAnimateControl::closeWindow()
{
    m_animateWindow->close();
}

void ClickAnimateControl::setDiameterColor( int diameter, QColor color)
{
    m_animateWindow->setRadiusColor( diameter, color );
}

void ClickAnimateControl::setShowTime( double value )
{
    m_showTime = value * 1000;
}

void ClickAnimateControl::setOpacity( double value )
{
    m_animateWindow->setOpacity( value );
}

void ClickAnimateControl::setRadiant( bool value )
{
    m_animateWindow->setRadiant( value );
}
